package com.qire.manhua.dal.service;

import com.qire.antsbinder.dal.DataService;
import com.qire.antsbinder.dal.annotation.StrategyAnnotation;
import com.qire.common.basic.ObjectUtil;
import com.qire.common.constant.Url;
import com.qire.common.dal.WarehouseFactory;
import com.qire.manhua.dal.ITradingDataDAL;
import com.qire.manhua.model.entity.ShareInfo;
import com.qire.manhua.model.viewModel.impl.FriendPaidViewModel;
import com.qire.manhua.model.vo.FriendPaidInfoVO;
import com.qire.manhua.model.vo.FriendPaidInfoVO.WaresVO;
import com.qire.manhua.model.vo.FriendPaidInfoVO.VipWareVO;
import com.qire.manhua.model.vo.FriendPaidOrderVO;
import com.qire.manhua.model.vo.FriendPaidOrderVO.ShareInfoVO;

public class FriendPaidService extends DataService {

    private final ITradingDataDAL tradingDataDAL = WarehouseFactory.create(this, ITradingDataDAL.class);

    private final FriendPaidViewModel viewModel;

    public FriendPaidService(FriendPaidViewModel viewModel) {
        this.viewModel = viewModel;
    }

    public void loadPaidData() {
        tradingDataDAL.fetchFriendPaidOptions();
    }
    @StrategyAnnotation(event = Url.PayRechargeOption)
    public void fetchFriendPaidOptionsApiCallback(FriendPaidInfoVO paidInfoVO, boolean isCache) {

        if(ObjectUtil.isNull(paidInfoVO)) {
            return;
        }

        boolean isFirstPay = paidInfoVO.is_first == 1;

        viewModel.clearCoinsPaidItemList();
        for(WaresVO waresVO : paidInfoVO.wares) {
            String waresFee = waresVO.waresFee();
            String waresDescribe =  waresVO.waresDescribe();
            String waresRewardFee = waresVO.waresRewardFee();
            boolean haveReward = waresVO.haveReward();
            viewModel.addCoinsPaidItem(waresVO.fee, isFirstPay, waresFee, waresDescribe, waresRewardFee, haveReward);
        }

        viewModel.clearMembershipPaidItemList();
        for(VipWareVO waresVO : paidInfoVO.vip_wares) {
            String waresFee = waresVO.waresFee();
            String waresDescribe = waresVO.waresDescribe();
            String waresRewardFee = waresVO.waresRewardFee();
            viewModel.addMembershipPaidItem(waresVO.fee, isFirstPay, waresFee, waresDescribe, waresRewardFee);
        }

        viewModel.refreshUI();
    }

    public void createFriendPaidOrder(int payType, int payFee) {
        tradingDataDAL.createFriendPaidOrder(payType, payFee);
    }
    @StrategyAnnotation(event = Url.CreateOrderPay)
    public void createFriendPaidOrderApiCallback(FriendPaidOrderVO orderVO, boolean isCache) {
        if(ObjectUtil.isNull(orderVO)) {
            return;
        }

        ShareInfoVO shareInfoVO = orderVO.share_info;
        if(ObjectUtil.nonNull(shareInfoVO)) {
            viewModel.setShareInfo(new ShareInfo(shareInfoVO.title, shareInfoVO.desc, shareInfoVO.img, shareInfoVO.link));
        }

        viewModel.refreshPaidUI(orderVO.order_money, orderVO.pay_img);

    }

}
